## An Experimental Analysis of the Prize–Probability Tradeoff in Stopping Problems
# Yair Antler and Ayala Arad (Tel Aviv University)
# 4.1 Alternative theory-based explanations: 
# Leave-one-out prediction exercise Replication Code

# Clear Global Environment and set working directory
rm(list=ls()) 

# Set working directory 
setwd("YOUR_FOLDER_PATH/stopping_rules_replication_folder")

## Package Install (install prior to load)
# install.packages("tidyverse")
# install.packages("lubridate")
# install.packages("rio")
# install.packages("bbmle")

# Load Packages 
library(tidyverse)
library(lubridate)
library(rio)
library(bbmle)

# loading function library 
source("Replication_scripts/stopping_rules_func_library/MaxLike_func_lib_script.R")

# Load Data
df <- import("Data/stopping_rules_data.csv")
data_regret <- import("Data/regret_numbers.csv")

#________________________________________________________________________________________________________________________________________________#

## leave-one-out cross validation exercise

# Two-Stage Qualitative Tradeoff Resolution - 2S-QTR Model
LOO_CV_2sQTR_full <- MaxLikeSR_2sQTR_LOO_crossVal(data=df, sub_sample="full", init=list(epsilon=0.4, alpha=0.4, beta=0.4))

# Constant Relative Risk Aversion (CRRA)
LOO_CV_CRRA_full <- MaxLikeSR_CRRA_LOO_crossVal(data=df, sub_sample="full", init=list(CRRA=0.3))

# Cumulative Prospect Theory (3 parameter model)
LOO_CV_CPT_3param_full <- MaxLikeSR_CPT_3param_LOO_crossVal(data=df, sub_sample="full", init=list(loss=2, delta = 0.2, alpha = 0.2))

# Cumulative Prospect Theory (5 parameter model)
LOO_CV_CPT_5param_full <- MaxLikeSR_CPT_5param_LOO_crossVal(data=df, sub_sample="full",
                                                            init=list(loss=2, alpha = 0.4, beta = 0.4, gamma = 0.6, delta = 0.6))
# Disappointment Aversion 
LOO_CV_disapAver_full <- MaxLikeSR_disapAver_LOO_crossVal(data=df, sub_sample="full", init=list(beta=2))

# Salience Theory
LOO_CV_salience_full <- MaxLikeSR_salience_LOO_crossVal(data=df, sub_sample="full", init=list(delta=0.4))

# # Regret Aversion 
LOO_CV_regretAver_full <- MaxLikeSR_regretAversion_LOO_crossVal(data=df, sub_sample = "full",
                                                                init=list(alpha=1, beta=2, sigma=0.2), df_regret_probs = data_regret)

# Rank Dependent Utility Prelec
LOO_CV_RDU_prelec_full <- MaxLikeSR_RDU_prelec_LOO_crossVal(data=df, sub_sample="full", init=list(RDU=1.5, b=1.5, alpha=0.6, loss = 2))

# Rank Dependent Utility Goldstein and Einhorn
LOO_CV_RDU_GE_full <- MaxLikeSR_RDU_GE_crossVal(data=df, sub_sample="full", init=list(b=1.1, beta=1.1, alpha=0.6, loss = 2))

# Rank Dependent Utility Kahneman and Tversky 
LOO_CV_RDU_KT_full <- MaxLikeSR_RDU_KT_LOO_crossVal(data=df, sub_sample="full", init=list(delta=0.6, alpha=0.6, loss=2))


# exporting results to excel
export(list(TwosQTR =    LOO_CV_2sQTR_full$summary,
            CRRA =       LOO_CV_CRRA_full$summary,
            CPT_3param = LOO_CV_CPT_3param_full$summary,
            CPT_5param = LOO_CV_CPT_5param_full$summary,
            disapAver =  LOO_CV_disapAver_full$summary,
            salience =   LOO_CV_salience_full$summary,
            regretAver = LOO_CV_regretAver_full$summary,
            RDU_prelec = LOO_CV_RDU_prelec_full$summary,
            RDU_GE =     LOO_CV_RDU_GE_full$summary,
            RDU_KT =     LOO_CV_RDU_KT_full$summary),
       "Stopping_rules_LOOCV_results_summary.xlsx")




